﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModBot.ConfigBuilder.Providers
{
    public class MeleeSettingsProvider
    {
        private ModBotSettingsProvider parentProvider;

        #region Public Properties
        /// <summary>
        /// Gets or sets a toggle for main assist target lock. When true 
        /// system will only attack the main assist target, otherwise
        /// it will first assist the main target and then not change targets
        /// until the target is dead.
        /// </summary>
        public Boolean OffTank { get; set; }

        /// <summary>
        /// Gets or sets the minimum distance to engage the mob. However, if
        /// "TankName" stays within 20 steps the bot will exceed this leash.
        /// </summary>
        public int ACLeash { get; set; }

        /// <summary>
        /// Gets or sets the percentage hitpoints the target must be below
        /// before assisting.
        /// </summary>
        public int ACAssistPct { get; set; }

        /// <summary>
        /// Gets or sets the percentage of mana at which to stop assisting.
        /// Set to 0 for melee only and 101 for non-melee casters.
        /// </summary>
        public int ACManaPct { get; set; }

        /// <summary>
        /// Gets or sets the cannel to announce in or if left
        /// blank disables annoucement.
        /// </summary>
        public string ACAnnounce { get; set; }

        /// <summary>
        /// Gets or sets the MQ2Melee commands
        /// </summary>
        public string ACMeleeCmd { get; set; }

        /// <summary>
        /// Gets or sets the script code to execute before enganging the target. 
        /// Note : Uses ModBot script code (same as MQ2 without "$").        
        /// </summary>        
        public string ACBefore { get; set; }

        /// <summary>
        /// Gets or sets the script code to execute after leaving combat.
        /// Note : Uses ModBot script code (same as MQ2 without "$").
        /// </summary>
        public string ACAfter { get; set; }
        #endregion

        private MeleeSettingsProvider() 
        { 
            this.OffTank = Defaults.Default_Melee_Offtank;
            this.ACLeash = Defaults.Default_Melee_ACLeash;
            this.ACAssistPct = Defaults.Default_Melee_ACAssistPct;
            this.ACManaPct = Defaults.Default_Melee_ACManaPct;
            this.ACAnnounce = Defaults.Default_Melee_ACAnnounce;
            this.ACMeleeCmd = Defaults.Default_Melee_ACMeleeCmd;
            this.ACBefore = Defaults.Default_Melee_ACBefore;
            this.ACAfter = Defaults.Default_Melee_ACAfter;
        }

        public MeleeSettingsProvider(ModBotSettingsProvider parentProvider)
        {
            this.parentProvider = parentProvider;
        }

        public override string ToString()
        {
            #region Format String
            string formatString =
                "[Melee]\r\n" +
                "OffTank={0}\r\n" +
                "ACLeash={1}\r\n" +
                "ACAssistPct={2}\r\n" +
                "ACAnnounce={3}\r\n" +
                "ACMeleeCmd={4}\r\n" +
                "ACBefore={5}\r\n" +
                "ACAfter={6}\r\n";
                
            #endregion

            return string.Format(formatString,
                OffTank,
                ACLeash,
                ACAssistPct,
                ACAnnounce,
                ACMeleeCmd,
                ACBefore,
                ACAfter);
        }

        public static MeleeSettingsProvider LoadFromINI(ModBotSettingsProvider settingsProvider, string filepath)
        {
            MeleeSettingsProvider provider = new MeleeSettingsProvider(settingsProvider);

            try
            {
                provider.OffTank = SharedLib.ConvertToBoolean(IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_OffTank), Defaults.Default_Melee_Offtank);
                provider.ACLeash = SharedLib.ConvertToInt32(IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACLeash), Defaults.Default_Melee_ACLeash);
                provider.ACAssistPct = SharedLib.ConvertToInt32(IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACAssistPct), Defaults.Default_Melee_ACAssistPct);
                provider.ACAnnounce = IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACAnnounce);
                provider.ACMeleeCmd = IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACMeleeCmd);
                provider.ACBefore = IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACBefore);
                provider.ACAfter = IniFileProvider.ReadValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACAfter);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return provider;
        }

        public static void SaveToIni(MeleeSettingsProvider provider, string filepath)
        {
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_OffTank, Convert.ToString(provider.OffTank));
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACLeash, Convert.ToString(provider.ACLeash));
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACAssistPct, Convert.ToString(provider.ACAssistPct));
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACAnnounce, Convert.ToString(provider.ACAnnounce));
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACMeleeCmd, Convert.ToString(provider.ACMeleeCmd));
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACBefore, Convert.ToString(provider.ACBefore));
            IniFileProvider.WriteValue(filepath, Keys.Melee_Section_Key, Keys.Melee_Key_ACAfter, Convert.ToString(provider.ACAfter));
        }
    }
}
